package com.esri.core.geometry;

import com.github.mikephil.charting.utils.Utils;

/* loaded from: classes.dex */
public final class Envelope2D {
    private final int a = 1;
    private final int b = 2;
    private final int c = 4;
    private final int d = 8;
    private final int e = 3;
    private final int f = 12;
    public double xmax;
    public double xmin;
    public double ymax;
    public double ymin;

    public static Envelope2D construct(double d, double d2, double d3, double d4) {
        Envelope2D envelope2D = new Envelope2D();
        envelope2D.xmin = d;
        envelope2D.ymin = d2;
        envelope2D.xmax = d3;
        envelope2D.ymax = d4;
        return envelope2D;
    }

    public Envelope2D() {
        setEmpty();
    }

    public Envelope2D(double d, double d2, double d3, double d4) {
        this.xmin = d;
        this.ymin = d2;
        this.xmax = d3;
        this.ymax = d4;
    }

    public void setCoords(double d, double d2) {
        this.xmin = d;
        this.ymin = d2;
        this.xmax = d;
        this.ymax = d2;
    }

    public void setCoords(double d, double d2, double d3, double d4) {
        this.xmin = d;
        this.ymin = d2;
        this.xmax = d3;
        this.ymax = d4;
        normalize();
    }

    public void setCoords(Point2D point2D, double d, double d2) {
        double d3 = point2D.x - (d * 0.5d);
        this.xmin = d3;
        this.xmax = d3 + d;
        double d4 = point2D.y - (0.5d * d2);
        this.ymin = d4;
        this.ymax = d4 + d2;
        normalize();
    }

    public void setCoords(Envelope2D envelope2D) {
        setCoords(envelope2D.xmin, envelope2D.ymin, envelope2D.xmax, envelope2D.ymax);
    }

    public void setFromPoints(Point2D[] point2DArr) {
        if (point2DArr == null || point2DArr.length == 0) {
            setEmpty();
            return;
        }
        Point2D point2D = point2DArr[0];
        setCoords(point2D.x, point2D.y);
        for (int i = 1; i < point2DArr.length; i++) {
            Point2D point2D2 = point2DArr[i];
            mergeNE(point2D2.x, point2D2.y);
        }
    }

    public void setEmpty() {
        this.xmin = Double.NaN;
        this.ymin = Double.NaN;
        this.xmax = Double.NaN;
        this.ymax = Double.NaN;
    }

    public void setInfinite() {
        this.xmin = NumberUtils.negativeInf();
        this.xmax = NumberUtils.positiveInf();
        this.ymin = NumberUtils.negativeInf();
        this.ymax = NumberUtils.positiveInf();
    }

    public boolean isEmpty() {
        return NumberUtils.isNaN(this.xmin);
    }

    public void setCoords(Envelope1D envelope1D, Envelope1D envelope1D2) {
        if (envelope1D.isEmpty() || envelope1D2.isEmpty()) {
            setEmpty();
            return;
        }
        this.xmin = envelope1D.vmin;
        this.xmax = envelope1D.vmax;
        this.ymin = envelope1D2.vmin;
        this.ymax = envelope1D2.vmax;
    }

    public void merge(double d, double d2) {
        if (isEmpty()) {
            this.xmin = d;
            this.ymin = d2;
            this.xmax = d;
            this.ymax = d2;
            return;
        }
        if (this.xmin > d) {
            this.xmin = d;
        } else if (this.xmax < d) {
            this.xmax = d;
        }
        if (this.ymin > d2) {
            this.ymin = d2;
        } else if (this.ymax < d2) {
            this.ymax = d2;
        }
    }

    public void mergeNE(double d, double d2) {
        if (this.xmin > d) {
            this.xmin = d;
        } else if (this.xmax < d) {
            this.xmax = d;
        }
        if (this.ymin > d2) {
            this.ymin = d2;
        } else if (this.ymax < d2) {
            this.ymax = d2;
        }
    }

    public void merge(Point2D point2D) {
        merge(point2D.x, point2D.y);
    }

    public void merge(Point3D point3D) {
        merge(point3D.x, point3D.y);
    }

    public void merge(Envelope2D envelope2D) {
        if (envelope2D.isEmpty()) {
            return;
        }
        merge(envelope2D.xmin, envelope2D.ymin);
        merge(envelope2D.xmax, envelope2D.ymax);
    }

    public void inflate(double d, double d2) {
        if (isEmpty()) {
            return;
        }
        double d3 = this.xmin - d;
        this.xmin = d3;
        double d4 = this.xmax + d;
        this.xmax = d4;
        double d5 = this.ymin - d2;
        this.ymin = d5;
        double d6 = this.ymax + d2;
        this.ymax = d6;
        if (d3 > d4 || d5 > d6) {
            setEmpty();
        }
    }

    public void scale(double d) {
        if (d < Utils.DOUBLE_EPSILON) {
            setEmpty();
        }
        if (isEmpty()) {
            return;
        }
        this.xmin *= d;
        this.xmax *= d;
        this.ymin *= d;
        this.ymax *= d;
    }

    public void zoom(double d, double d2) {
        if (isEmpty()) {
            return;
        }
        setCoords(getCenter(), d * getWidth(), d2 * getHeight());
    }

    public boolean isIntersecting(Envelope2D envelope2D) {
        if (!isEmpty() && !envelope2D.isEmpty()) {
            double d = this.xmin;
            double d2 = envelope2D.xmin;
            if (d > d2 ? envelope2D.xmax >= d : this.xmax >= d2) {
                double d3 = this.ymin;
                double d4 = envelope2D.ymin;
                if (d3 > d4 ? envelope2D.ymax >= d3 : this.ymax >= d4) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean isIntersectingNE(Envelope2D envelope2D) {
        double d = this.xmin;
        double d2 = envelope2D.xmin;
        if (d > d2 ? envelope2D.xmax >= d : this.xmax >= d2) {
            double d3 = this.ymin;
            double d4 = envelope2D.ymin;
            if (d3 > d4 ? envelope2D.ymax >= d3 : this.ymax >= d4) {
                return true;
            }
        }
        return false;
    }

    public boolean intersect(Envelope2D envelope2D) {
        boolean z = false;
        if (!isEmpty() && !envelope2D.isEmpty()) {
            double d = envelope2D.xmin;
            if (d > this.xmin) {
                this.xmin = d;
            }
            double d2 = envelope2D.xmax;
            if (d2 < this.xmax) {
                this.xmax = d2;
            }
            double d3 = envelope2D.ymin;
            if (d3 > this.ymin) {
                this.ymin = d3;
            }
            double d4 = envelope2D.ymax;
            if (d4 < this.ymax) {
                this.ymax = d4;
            }
            if (this.xmin <= this.xmax && this.ymin <= this.ymax) {
                z = true;
            }
            if (!z) {
                setEmpty();
            }
        }
        return z;
    }

    public Point2D queryCorner(int i) {
        if (i != 0) {
            if (i != 1) {
                if (i != 2) {
                    if (i == 3) {
                        return Point2D.construct(this.xmax, this.ymin);
                    }
                    throw new IndexOutOfBoundsException();
                }
                return Point2D.construct(this.xmax, this.ymax);
            }
            return Point2D.construct(this.xmin, this.ymax);
        }
        return Point2D.construct(this.xmin, this.ymin);
    }

    public void queryCorners(Point2D[] point2DArr) {
        if (point2DArr == null || point2DArr.length < 4) {
            throw new IllegalArgumentException();
        }
        point2DArr[0] = new Point2D(this.xmin, this.ymin);
        point2DArr[1] = new Point2D(this.xmin, this.ymax);
        point2DArr[2] = new Point2D(this.xmax, this.ymax);
        point2DArr[3] = new Point2D(this.xmax, this.ymin);
    }

    public void queryCornersReversed(Point2D[] point2DArr) {
        if (point2DArr == null || (point2DArr != null && point2DArr.length < 4)) {
            throw new IllegalArgumentException();
        }
        point2DArr[0] = new Point2D(this.xmin, this.ymin);
        point2DArr[1] = new Point2D(this.xmax, this.ymin);
        point2DArr[2] = new Point2D(this.xmax, this.ymax);
        point2DArr[3] = new Point2D(this.xmin, this.ymax);
    }

    public double getArea() {
        return isEmpty() ? Utils.DOUBLE_EPSILON : getWidth() * getHeight();
    }

    public double getLength() {
        return isEmpty() ? Utils.DOUBLE_EPSILON : (getWidth() + getHeight()) * 2.0d;
    }

    public void setFromPoints(Point2D[] point2DArr, int i) {
        if (i == 0) {
            setEmpty();
            return;
        }
        this.xmin = point2DArr[0].x;
        double d = point2DArr[0].y;
        this.ymin = d;
        this.xmax = this.xmin;
        this.ymax = d;
        for (int i2 = 1; i2 < i; i2++) {
            Point2D point2D = point2DArr[i2];
            if (point2D.x < this.xmin) {
                this.xmin = point2D.x;
            } else if (point2D.x > this.xmax) {
                this.xmax = point2D.x;
            }
            if (point2D.y < this.ymin) {
                this.ymin = point2D.y;
            } else if (point2D.y > this.ymax) {
                this.ymax = point2D.y;
            }
        }
    }

    public void reaspect(double d, double d2) {
        if (isEmpty()) {
            return;
        }
        double d3 = d / d2;
        double width = getWidth() * 0.5d;
        double height = getHeight() * 0.5d * d3;
        if (width <= height) {
            double centerX = getCenterX();
            this.xmin = centerX - height;
            this.xmax = centerX + height;
        } else {
            double d4 = width / d3;
            double centerY = getCenterY();
            this.ymin = centerY - d4;
            this.ymax = centerY + d4;
        }
        normalize();
    }

    public double getCenterX() {
        return (this.xmax + this.xmin) / 2.0d;
    }

    public double getCenterY() {
        return (this.ymax + this.ymin) / 2.0d;
    }

    public double getWidth() {
        return this.xmax - this.xmin;
    }

    public double getHeight() {
        return this.ymax - this.ymin;
    }

    public void move(double d, double d2) {
        if (isEmpty()) {
            return;
        }
        this.xmin += d;
        this.ymin += d2;
        this.xmax += d;
        this.ymax += d2;
    }

    public void centerAt(double d, double d2) {
        move(d - getCenterX(), d2 - getCenterY());
    }

    public void centerAt(Point2D point2D) {
        centerAt(point2D.x, point2D.y);
    }

    public void offset(double d, double d2) {
        this.xmin += d;
        this.xmax += d;
        this.ymin += d2;
        this.ymax += d2;
    }

    public void normalize() {
        if (isEmpty()) {
            return;
        }
        double min = Math.min(this.xmin, this.xmax);
        double max = Math.max(this.xmin, this.xmax);
        this.xmin = min;
        this.xmax = max;
        double min2 = Math.min(this.ymin, this.ymax);
        double max2 = Math.max(this.ymin, this.ymax);
        this.ymin = min2;
        this.ymax = max2;
    }

    public boolean isValid() {
        return (NumberUtils.isNaN(this.xmin) || NumberUtils.isNaN(this.xmax) || NumberUtils.isNaN(this.ymin) || NumberUtils.isNaN(this.ymax) || this.xmin > this.xmax || this.ymin > this.ymax) ? false : true;
    }

    public Point2D getCenter() {
        return new Point2D((this.xmax + this.xmin) / 2.0d, (this.ymax + this.ymin) / 2.0d);
    }

    public void queryCenter(Point2D point2D) {
        point2D.x = (this.xmax + this.xmin) / 2.0d;
        point2D.y = (this.ymax + this.ymin) / 2.0d;
    }

    public void centerAt(Point point) {
        double d = (this.xmax - this.xmin) / 2.0d;
        double d2 = (this.ymax - this.ymin) / 2.0d;
        this.xmin = point.getX() - d;
        this.xmax = point.getX() + d;
        this.ymin = point.getY() - d2;
        this.ymax = point.getY() + d2;
    }

    public Point getLowerLeft() {
        return new Point(this.xmin, this.ymin);
    }

    public Point getUpperLeft() {
        return new Point(this.xmin, this.ymax);
    }

    public Point getLowerRight() {
        return new Point(this.xmax, this.ymin);
    }

    public Point getUpperRight() {
        return new Point(this.xmax, this.ymax);
    }

    public boolean contains(Point point) {
        return contains(point.getX(), point.getY());
    }

    public boolean contains(Point2D point2D) {
        return contains(point2D.x, point2D.y);
    }

    public boolean contains(double d, double d2) {
        return !isEmpty() && d >= this.xmin && d <= this.xmax && d2 >= this.ymin && d2 <= this.ymax;
    }

    public boolean contains(Envelope2D envelope2D) {
        return envelope2D.xmin >= this.xmin && envelope2D.xmax <= this.xmax && envelope2D.ymin >= this.ymin && envelope2D.ymax <= this.ymax;
    }

    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj instanceof Envelope2D) {
            Envelope2D envelope2D = (Envelope2D) obj;
            if (isEmpty() && envelope2D.isEmpty()) {
                return true;
            }
            return this.xmin == envelope2D.xmin && this.ymin == envelope2D.ymin && this.xmax == envelope2D.xmax && this.ymax == envelope2D.ymax;
        }
        return false;
    }

    public int hashCode() {
        long doubleToLongBits = Double.doubleToLongBits(this.xmin);
        int hash = NumberUtils.hash((int) (doubleToLongBits ^ (doubleToLongBits >>> 32)));
        long doubleToLongBits2 = Double.doubleToLongBits(this.xmax);
        int hash2 = NumberUtils.hash(hash, (int) (doubleToLongBits2 ^ (doubleToLongBits2 >>> 32)));
        long doubleToLongBits3 = Double.doubleToLongBits(this.ymin);
        int hash3 = NumberUtils.hash(hash2, (int) (doubleToLongBits3 ^ (doubleToLongBits3 >>> 32)));
        long doubleToLongBits4 = Double.doubleToLongBits(this.ymax);
        return NumberUtils.hash(hash3, (int) ((doubleToLongBits4 >>> 32) ^ doubleToLongBits4));
    }

    Point2D a(Point2D point2D) {
        double d;
        double d2;
        double d3;
        double d4;
        Point2D point2D2 = new Point2D();
        point2D2.setCoords(point2D);
        if (point2D2.e()) {
            return point2D2;
        }
        if (isEmpty()) {
            point2D2.d();
            return point2D2;
        }
        double d5 = point2D2.x;
        double d6 = this.xmin;
        if (d5 < d6) {
            point2D2.x = d6;
        } else {
            double d7 = point2D2.x;
            double d8 = this.xmax;
            if (d7 > d8) {
                point2D2.x = d8;
            }
        }
        double d9 = point2D2.y;
        double d10 = this.ymin;
        if (d9 < d10) {
            point2D2.y = d10;
        } else {
            double d11 = point2D2.y;
            double d12 = this.ymax;
            if (d11 > d12) {
                point2D2.y = d12;
            }
        }
        if (point2D2.equals(point2D)) {
            Point2D center = getCenter();
            if (point2D2.x < center.x) {
                d = point2D2.x;
                d2 = this.xmin;
            } else {
                d = this.xmax;
                d2 = point2D2.x;
            }
            double d13 = d - d2;
            if (point2D2.y < center.y) {
                d3 = point2D2.y;
                d4 = this.ymin;
            } else {
                d3 = this.ymax;
                d4 = point2D2.y;
            }
            if (d13 < d3 - d4) {
                point2D2.x = point2D2.x < center.x ? this.xmin : this.xmax;
            } else {
                point2D2.y = point2D2.y < center.y ? this.ymin : this.ymax;
            }
            return point2D2;
        }
        return point2D2;
    }

    public double _boundaryDistance(Point2D point2D) {
        if (isEmpty()) {
            return NumberUtils.NaN();
        }
        double d = point2D.x;
        double d2 = this.xmin;
        if (d == d2) {
            return point2D.y - this.ymin;
        }
        double d3 = this.ymax - this.ymin;
        double d4 = this.xmax - d2;
        if (point2D.y == this.ymax) {
            return (d3 + point2D.x) - this.xmin;
        }
        if (point2D.x == this.xmax) {
            return ((d3 + d4) + this.ymax) - point2D.y;
        }
        if (point2D.y == this.ymin) {
            return (((d3 * 2.0d) + d4) + this.xmax) - point2D.x;
        }
        return _boundaryDistance(a(point2D));
    }

    public int _envelopeSide(Point2D point2D) {
        if (isEmpty()) {
            return -1;
        }
        double _boundaryDistance = _boundaryDistance(point2D);
        double d = this.ymax - this.ymin;
        double d2 = this.xmax - this.xmin;
        if (_boundaryDistance < d) {
            return 0;
        }
        double d3 = _boundaryDistance - d;
        if (d3 < d2) {
            return 1;
        }
        return d3 - d2 < d ? 2 : 3;
    }

    public String toString() {
        return "Envelope2D [xmin=" + this.xmin + ", ymin=" + this.ymin + ", xmax=" + this.xmax + ", ymax=" + this.ymax + "]";
    }

    double a() {
        double abs;
        if (isEmpty()) {
            abs = NumberUtils.doubleEps();
        } else {
            abs = (Math.abs(this.xmin) + Math.abs(this.xmax) + Math.abs(this.ymin) + Math.abs(this.ymax) + 1.0d) * NumberUtils.doubleEps();
        }
        return abs * 100.0d;
    }

    public int a(Point2D point2D, Point2D point2D2) {
        int b = b(point2D);
        int b2 = b(point2D2);
        if ((b & b2) != 0) {
            return 0;
        }
        if ((b | b2) == 0) {
            return 4;
        }
        int i = (b != 0 ? 1 : 0) | (b2 != 0 ? 2 : 0);
        while (true) {
            double d = point2D2.x - point2D.x;
            double d2 = point2D2.y - point2D.y;
            if (d > d2) {
                if ((b & 3) != 0) {
                    if ((b & 1) != 0) {
                        point2D.y += (d2 * (this.xmin - point2D.x)) / d;
                        point2D.x = this.xmin;
                    } else {
                        point2D.y += (d2 * (this.xmax - point2D.x)) / d;
                        point2D.x = this.xmax;
                    }
                    b = b(point2D);
                } else if ((b2 & 3) != 0) {
                    if ((b2 & 1) != 0) {
                        point2D2.y += (d2 * (this.xmin - point2D2.x)) / d;
                        point2D2.x = this.xmin;
                    } else {
                        point2D2.y += (d2 * (this.xmax - point2D2.x)) / d;
                        point2D2.x = this.xmax;
                    }
                    b2 = b(point2D2);
                } else if (b != 0) {
                    if ((b & 4) != 0) {
                        point2D.x += (d * (this.ymin - point2D.y)) / d2;
                        point2D.y = this.ymin;
                    } else {
                        point2D.x += (d * (this.ymax - point2D.y)) / d2;
                        point2D.y = this.ymax;
                    }
                    b = b(point2D);
                } else {
                    if ((b2 & 4) != 0) {
                        point2D2.x += (d * (this.ymin - point2D2.y)) / d2;
                        point2D2.y = this.ymin;
                    } else {
                        point2D2.x += (d * (this.ymax - point2D2.y)) / d2;
                        point2D2.y = this.ymax;
                    }
                    b2 = b(point2D2);
                }
            } else if ((b & 12) != 0) {
                if ((b & 4) != 0) {
                    point2D.x += (d * (this.ymin - point2D.y)) / d2;
                    point2D.y = this.ymin;
                } else {
                    point2D.x += (d * (this.ymax - point2D.y)) / d2;
                    point2D.y = this.ymax;
                }
                b = b(point2D);
            } else if ((b2 & 12) != 0) {
                if ((b2 & 4) != 0) {
                    point2D2.x += (d * (this.ymin - point2D2.y)) / d2;
                    point2D2.y = this.ymin;
                } else {
                    point2D2.x += (d * (this.ymax - point2D2.y)) / d2;
                    point2D2.y = this.ymax;
                }
                b2 = b(point2D2);
            } else if (b != 0) {
                if ((b & 1) != 0) {
                    point2D.y += (d2 * (this.xmin - point2D.x)) / d;
                    point2D.x = this.xmin;
                } else {
                    point2D.y += (d2 * (this.xmax - point2D.x)) / d;
                    point2D.x = this.xmax;
                }
                b = b(point2D);
            } else {
                if ((b2 & 1) != 0) {
                    point2D2.y += (d2 * (this.xmin - point2D2.x)) / d;
                    point2D2.x = this.xmin;
                } else {
                    point2D2.y += (d2 * (this.xmax - point2D2.x)) / d;
                    point2D2.x = this.xmax;
                }
                b2 = b(point2D2);
            }
            if ((b & b2) != 0) {
                return 0;
            }
            if ((b | b2) == 0) {
                return i;
            }
        }
    }

    int b(Point2D point2D) {
        return ((point2D.x > this.xmax ? 1 : 0) << 1) | (point2D.x < this.xmin ? 1 : 0) | ((point2D.y < this.ymin ? 1 : 0) << 2) | ((point2D.y > this.ymax ? 1 : 0) << 3);
    }

    public int a(Point2D point2D, Point2D point2D2, int i, double[] dArr, double[] dArr2) {
        if (dArr2 != null) {
            dArr2[0] = -1.0d;
            dArr2[1] = -1.0d;
        }
        int i2 = 2;
        double[] dArr3 = new double[2];
        Point2D point2D3 = new Point2D(point2D2.x - point2D.x, point2D2.y - point2D.y);
        double d = point2D3.x;
        double d2 = Utils.DOUBLE_EPSILON;
        if (d == Utils.DOUBLE_EPSILON && point2D3.y == Utils.DOUBLE_EPSILON) {
            dArr[0] = 0.0d;
            dArr[1] = 0.0d;
            return contains(point2D) ? 4 : 0;
        }
        if ((i & 1) != 0) {
            d2 = NumberUtils.negativeInf();
        }
        dArr[0] = d2;
        dArr[1] = (i & 2) != 0 ? NumberUtils.positiveInf() : 1.0d;
        dArr3[0] = dArr[0];
        dArr3[1] = dArr[1];
        if (a(point2D3.x, this.xmin - point2D.x, dArr) && a(-point2D3.x, point2D.x - this.xmax, dArr) && a(point2D3.y, this.ymin - point2D.y, dArr) && a(-point2D3.y, point2D.y - this.ymax, dArr)) {
            if (dArr[1] < dArr3[1]) {
                point2D2.scaleAdd(dArr[1], point2D3, point2D);
                a(point2D2);
                if (dArr2 != null) {
                    dArr2[1] = _boundaryDistance(point2D2);
                }
            } else {
                i2 = 0;
            }
            if (dArr[0] > dArr3[0]) {
                point2D.scaleAdd(dArr[0], point2D3, point2D);
                a(point2D);
                int i3 = i2 | 1;
                if (dArr2 != null) {
                    dArr2[0] = _boundaryDistance(point2D);
                }
                return i3;
            }
            return i2;
        }
        return 0;
    }

    boolean a(double d, double d2, double[] dArr) {
        double d3 = d2 / d;
        if (d > Utils.DOUBLE_EPSILON) {
            if (d3 > dArr[1]) {
                return false;
            }
            if (d3 > dArr[0]) {
                dArr[0] = d3;
                return true;
            }
        } else if (d >= Utils.DOUBLE_EPSILON) {
            return d2 <= Utils.DOUBLE_EPSILON;
        } else if (d3 < dArr[0]) {
            return false;
        } else {
            if (d3 < dArr[1]) {
                dArr[1] = d3;
            }
        }
        return true;
    }

    boolean a(double d) {
        return !isEmpty() && (getWidth() <= d || getHeight() <= d);
    }

    Point2D c(Point2D point2D) {
        return new Point2D(NumberUtils.snap(point2D.x, this.xmin, this.xmax), NumberUtils.snap(point2D.y, this.ymin, this.ymax));
    }
}
